package com.lvyh.lightframe.log.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.slf4j.MDC;

import java.util.UUID;

/**
 * @author lvyh 2021/03/20.
 */
public class MethodInterceptorLogTraceAdvice implements MethodInterceptor {
    private final String traceId = "traceId";

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {

        MDC.put(this.traceId, UUID.randomUUID().toString().replace("-", "").toLowerCase());
        Object object = null;
        try {
            object = invocation.proceed();
            return object;
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        } finally {
            MDC.clear();
        }

    }
}
